﻿using System.Net.Http.Json;
using Aurora.AI.Abstractions;
using Aurora.AI.Abstractions.Embeddings;
using Aurora.AI.Abstractions.Extensions;
using Aurora.AI.Abstractions.ObjectModels.RequestModels;
using Aurora.AI.Abstractions.ObjectModels.ResponseModels;

namespace Aurora.AI.Moonshot.Embeddings;

public sealed class MoonshotTextEmbeddingService(IHttpClientFactory httpClientFactory)
    : IAuroraAITextEmbeddingService
{
    public async Task<EmbeddingCreateResponse> EmbeddingAsync(EmbeddingCreateRequest createEmbeddingModel,
        AuroraAIPlatformOptions? options = null,
        CancellationToken cancellationToken = default)
    {
        var client = httpClientFactory.CreateClient(MoonshotPlatformOptions.PlatformCode);

        var response = await client.PostJsonAsync(options?.Address.TrimEnd('/') + "/v1/embeddings",
            createEmbeddingModel, options!.ApiKey);

        var result =
            await response.Content.ReadFromJsonAsync<EmbeddingCreateResponse>(cancellationToken: cancellationToken);

        return result;
    }
}